<template>
  <div class="login-page">
    <Header />

    <!-- 登录内容区域 -->
    <div class="login-content">
      <div class="container">
        <div class="login-wrapper">
          <!-- 左侧品牌信息 -->
          <div class="brand-section">
            <div class="brand-content">
              <img src="@/assets/logo-full.png" alt="梧州六堡茶" class="brand-logo" />
              <h2 class="brand-title">欢迎回来</h2>
              <p class="brand-desc">登录您的账户，继续享受优质六堡茶</p>
              <div class="brand-features">
                <div class="feature-item">
                  <span class="feature-icon">🍃</span>
                  <span class="feature-text">正宗六堡茶</span>
                </div>
                <div class="feature-item">
                  <span class="feature-icon">🚚</span>
                  <span class="feature-text">全国包邮</span>
                </div>
                <div class="feature-item">
                  <span class="feature-icon">💎</span>
                  <span class="feature-text">品质保证</span>
                </div>
              </div>
            </div>
          </div>

          <!-- 右侧登录表单 -->
          <div class="form-section">
            <div class="form-container">
              <div class="form-header">
                <h3 class="form-title">用户登录</h3>
                <p class="form-subtitle">请输入您的账号信息</p>
              </div>

              <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" class="login-form" @submit.prevent="handleLogin">
                <el-form-item prop="username">
                  <el-input v-model="loginForm.username" placeholder="请输入用户名/手机号" size="large" prefix-icon="User" clearable />
                </el-form-item>

                <el-form-item prop="password">
                  <el-input
                    v-model="loginForm.password"
                    type="password"
                    placeholder="请输入密码"
                    size="large"
                    prefix-icon="Lock"
                    show-password
                    clearable />
                </el-form-item>

                <div class="form-options">
                  <el-checkbox v-model="loginForm.remember">记住我</el-checkbox>
                  <router-link to="/forgot-password" class="forgot-link">忘记密码？</router-link>
                </div>

                <el-form-item>
                  <el-button type="primary" size="large" class="login-btn" :loading="loading" @click="handleLogin">
                    {{ loading ? '登录中...' : '登录' }}
                  </el-button>
                </el-form-item>

                <div class="form-footer">
                  <span class="register-text">还没有账户？</span>
                  <router-link to="/register" class="register-link">立即注册</router-link>
                </div>
              </el-form>

              <!-- 第三方登录 -->
              <div class="social-login">
                <div class="divider">
                  <span class="divider-text">或使用以下方式登录</span>
                </div>
                <div class="social-buttons">
                  <button class="social-btn wechat-btn" @click="wechatLogin">
                    <span class="social-icon">💬</span>
                    微信登录
                  </button>
                  <button class="social-btn qq-btn" @click="qqLogin">
                    <span class="social-icon">🐧</span>
                    QQ登录
                  </button>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <Footer />
  </div>
</template>

<script setup>
  import { ref, reactive } from 'vue'
  import { useRouter } from 'vue-router'
  import { ElMessage } from 'element-plus'
  import Header from '@/components/Header.vue'
  import Footer from '@/components/Footer.vue'
  import { useUserStore } from '@/stores/user.js'

  const router = useRouter()
  const userStore = useUserStore()
  const loginFormRef = ref()
  const loading = ref(false)

  // 登录表单数据
  const loginForm = reactive({
    username: '',
    password: '',
    remember: false,
  })

  // 表单验证规则
  const loginRules = {
    username: [
      { required: true, message: '请输入用户名或手机号', trigger: 'blur' },
      { min: 3, max: 20, message: '用户名长度在 3 到 20 个字符', trigger: 'blur' },
    ],
    password: [
      { required: true, message: '请输入密码', trigger: 'blur' },
      { min: 6, max: 20, message: '密码长度在 6 到 20 个字符', trigger: 'blur' },
    ],
  }

  // 处理登录
  const handleLogin = async () => {
    if (!loginFormRef.value) return

    try {
      const valid = await loginFormRef.value.validate()
      if (!valid) return

      loading.value = true

      // 调用用户store的登录方法
      const result = await userStore.login({
        username: loginForm.username,
        password: loginForm.password,
        remember: loginForm.remember,
      })

      if (result.success) {
        ElMessage.success('登录成功！')

        // 登录成功后跳转到用户中心或首页
        const redirect = router.currentRoute.value.query.redirect || '/user'
        router.push(redirect)
      } else {
        ElMessage.error(result.error || '登录失败，请检查用户名和密码')
      }
    } catch (error) {
      console.error('登录失败:', error)
      ElMessage.error('登录失败，请稍后重试')
    } finally {
      loading.value = false
    }
  }

  // 微信登录
  const wechatLogin = () => {
    ElMessage.info('微信登录功能开发中...')
  }

  // QQ登录
  const qqLogin = () => {
    ElMessage.info('QQ登录功能开发中...')
  }
</script>

<style scoped>
  .login-page {
    min-height: 100vh;
    background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  }

  .login-content {
    padding: 60px 0;
    min-height: calc(100vh - 200px);
    display: flex;
    align-items: center;
  }

  .container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 0 20px;
  }

  .login-wrapper {
    display: flex;
    background: white;
    border-radius: 16px;
    box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
    overflow: hidden;
    min-height: 600px;
  }

  /* 左侧品牌区域 */
  .brand-section {
    flex: 1;
    background: linear-gradient(135deg, #1c705e 0%, #2d8a6e 100%);
    color: white;
    padding: 60px 40px;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .brand-content {
    text-align: center;
    max-width: 400px;
  }

  .brand-logo {
    width: 120px;
    height: auto;
    margin-bottom: 30px;
    background: white;
    padding: 15px;
    border-radius: 12px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  }

  .brand-title {
    font-size: 32px;
    font-weight: 600;
    margin-bottom: 16px;
    color: white;
  }

  .brand-desc {
    font-size: 16px;
    opacity: 0.9;
    margin-bottom: 40px;
    line-height: 1.6;
  }

  .brand-features {
    display: flex;
    flex-direction: column;
    gap: 20px;
  }

  .feature-item {
    display: flex;
    align-items: center;
    gap: 12px;
    font-size: 14px;
  }

  .feature-icon {
    font-size: 20px;
  }

  /* 右侧表单区域 */
  .form-section {
    flex: 1;
    padding: 60px 40px;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .form-container {
    width: 100%;
    max-width: 400px;
  }

  .form-header {
    text-align: center;
    margin-bottom: 40px;
  }

  .form-title {
    font-size: 28px;
    font-weight: 600;
    color: #333;
    margin-bottom: 8px;
  }

  .form-subtitle {
    color: #666;
    font-size: 14px;
  }

  .login-form {
    margin-bottom: 30px;
  }

  .login-form .el-form-item {
    margin-bottom: 24px;
  }

  .form-options {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 24px;
  }

  .forgot-link {
    color: #1c705e;
    text-decoration: none;
    font-size: 14px;
  }

  .forgot-link:hover {
    text-decoration: underline;
  }

  .login-btn {
    width: 100%;
    height: 48px;
    font-size: 16px;
    font-weight: 600;
    background: #1c705e;
    border-color: #1c705e;
  }

  .login-btn:hover {
    background: #2d8a6e;
    border-color: #2d8a6e;
  }

  .form-footer {
    text-align: center;
    font-size: 14px;
    color: #666;
  }

  .register-link {
    color: #1c705e;
    text-decoration: none;
    font-weight: 600;
    margin-left: 8px;
  }

  .register-link:hover {
    text-decoration: underline;
  }

  /* 第三方登录 */
  .social-login {
    margin-top: 30px;
  }

  .divider {
    text-align: center;
    margin: 20px 0;
    position: relative;
  }

  .divider::before {
    content: '';
    position: absolute;
    top: 50%;
    left: 0;
    right: 0;
    height: 1px;
    background: #e0e0e0;
  }

  .divider-text {
    background: white;
    padding: 0 16px;
    color: #999;
    font-size: 12px;
  }

  .social-buttons {
    display: flex;
    gap: 12px;
  }

  .social-btn {
    flex: 1;
    height: 44px;
    border: 1px solid #e0e0e0;
    border-radius: 8px;
    background: white;
    color: #666;
    font-size: 14px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    transition: all 0.3s;
  }

  .social-btn:hover {
    border-color: #1c705e;
    color: #1c705e;
  }

  .social-icon {
    font-size: 16px;
  }

  /* 响应式设计 */
  @media (max-width: 768px) {
    .login-wrapper {
      flex-direction: column;
      margin: 20px;
    }

    .brand-section {
      padding: 40px 20px;
    }

    .form-section {
      padding: 40px 20px;
    }

    .brand-title {
      font-size: 24px;
    }

    .form-title {
      font-size: 24px;
    }

    .social-buttons {
      flex-direction: column;
    }
  }

  @media (max-width: 480px) {
    .login-content {
      padding: 20px 0;
    }

    .container {
      padding: 0 10px;
    }

    .brand-section,
    .form-section {
      padding: 30px 15px;
    }
  }
</style>
